﻿// JScript 文件
///用户Activities相关模块
/*
 *   表单助理
 *   helper=0               该控件绑定助理信息
 *   hvalue                 该控件所对应助理信息的值
 *   hotype                 助理信息类型 0 普通输入框，1 选择模式，2 加载对应的实体将页面转变为修改模式
 *   hcontrols              助理信息所绑定控件的名称
 *   fields                 助理信息检索所需字段
 *   LoadingHelperData()    助理信息数据绑定事件
 *   hneed                  助理信息是否绑定了控件 0 否，1 是
 *   tabid                  所在选单的Id，如没有选单，则该值设为tabid="no"
 *   
 *   控件--> <input id="txtCustomerName" helper="0" hotype="0" hvalue="Name">
 *   
 *   筛选列表-->  <select class="ms-crm-SelectBox ContextSelect " id="ContextSelect" tabindex="1190">
 *                      <option value="Name" hneed="1" hcontrols="txtCustomerName" fields="Name" hotype="0" tabid="tab0">客户名称</option>
 *                  </select>
*/

var HELP_LookUp_Otype="";                   ///控件类型
var HELP_LookUp_Controls="";                ///对应控件
var HELP_LookUp_Key="";                     ///对应字段
var HELP_LookUp_TableIndex="";              ///对应数据表序列
var HELP_LookUp_TabId="";                   ///选单ID
var HELP_LookUp_IsFilter="";                ///是否过滤已删除项
var HELP_LookUp_ImgType="";                 ///图片类型（列开头图片）

$(document).ready(function(){
   $("#divContentList").html($("#divNothing").html());
   $('#ContextSelect').bind("change", function(){
        var hneed=$('#ContextSelect').children('[value=' + $(this).val() + ']').attr('hneed');
        if(hneed == "1")
        {
            $('#Category_Lookup').css('display','inline');
            $('#findValue').val("");
            var _LookKey=$(this).val();
            SetHelperValues(_LookKey);
            $('#' + HELP_LookUp_Controls).focus();
            LoadingHelperData();
            if(HELP_LookUp_TabId != "no")
            {
                $("#" + HELP_LookUp_TabId + "Tab").click();
            }
            if(HELP_LookUp_Controls == "divCustomer")
            {
                $('#tdCustomer').css("display","inline");
            }else{
                $('#tdCustomer').css("display","none");
            }
        }
        else
        {
            $('#Category_Lookup').css('display','none');
        }
   });
   ///绑定帮助事件
   $('[helper=0]').each(function(){
        var _LookKey = "";
        if($(this).attr('hotype') == "0")
        {
            $(this).bind('focus',function(){
                var hidedown=$('#ShowHideImage').attr('alt');
                if(hidedown == "单击以打开表单助理")
                {
                    $('#ShowHideImage').click();
                }
                _LookKey= $(this).attr('hvalue');
                $('#ContextSelect').val(_LookKey);
                SetHelperValues(_LookKey);
                $('#findValue').val($(this).val());
                //LoadingHelperData();
            }).bind('keyup', function(){
                $('#findValue').val($(this).val());
            });
        }
        if($(this).attr('hotype') == "1")
            $(this).bind('click',function(){
                var hidedown=$('#ShowHideImage').attr('alt');
                if(hidedown == "单击以打开表单助理")
                {
                     $('#ShowHideImage').click();
                }

                $('#ContextSelect').val($(this).attr('hvalue'));
                _LookKey = $(this).attr('hvalue');
                SetHelperValues(_LookKey);
                LoadingHelperData();
                
                if(HELP_LookUp_Controls == "divCustomer")
                {
                    $('#tdCustomer').css("display","inline");
                }else{
                    $('#tdCustomer').css("display","none");
                }
            });
        if($(this).attr('hotype') == "2")
        {
            $(this).bind('focus',function(){
                var hidedown=$('#ShowHideImage').attr('alt');
                if(hidedown == "单击以打开表单助理")
                {
                    $('#ShowHideImage').click();
                }
                _LookKey= $(this).attr('hvalue');
                $('#ContextSelect').val(_LookKey);
                SetHelperValues(_LookKey);
                $('#findValue').val($(this).val());
                LoadingHelperData();
            }).bind('keyup',function(){
                $('#findValue').val($(this).val());
            });
        }
   });
   $('#findCriteriaImg').bind('click',function(){
        if($('#ContextSelect').val() != "RIHelpContext")
            LoadingHelperData();
   });
})
///控件赋值
function SetControlsValues(obj)
{
    switch(HELP_LookUp_Otype)
    {
        case "0":
            $('#' + HELP_LookUp_Controls).val(obj.innerText);
            break;
        case "1":
            if(!$('#' + HELP_LookUp_Controls).attr('forbidden'))
            {
                if(HELP_LookUp_Controls == "divCustomer")
                {
                    $('#txtToPerson').val(obj.keysfile1);
                    $('#txtPhoneNumber').val(obj.keysfile2);
                }
                var oid = obj.oid;
                var _contentHTML="<span contenteditable='false' class='ms-crm-Lookup-Item' oid=" + oid + " otype='8' otypename='systemuser'>" + obj.innerHTML + "</span>";
                $('#' + HELP_LookUp_Controls).html(_contentHTML);
                var _idControl=$('#' + HELP_LookUp_Controls).attr('oid');
                $('#' + _idControl).val(oid).click();
            }
            break;
        case "2":
            var _url=window.location.pathname;
            _url  += "?otype=modify&" + HELPER_PAGEID + "=" + obj.oid;
            window.location.href=_url;
            break;
    }
}
///参数赋值
function SetHelperValues(lookkey)
{
    HELP_LookUp_Otype=$('#ContextSelect').children('[value=' + lookkey + ']').attr('hotype');
    HELP_LookUp_Controls=$('#ContextSelect').children('[value=' + lookkey + ']').attr('hcontrols');
    HELP_LookUp_Key=$('#ContextSelect').children('[value=' + lookkey + ']').attr('fields');
    HELP_LookUp_TableIndex=$('#ContextSelect').children('[value=' + lookkey + ']').attr('tindex');
    HELP_LookUp_TabId=$('#ContextSelect').children('[value=' + lookkey + ']').attr('tabid');
    HELP_LookUp_ImgType=$('#ContextSelect').children('[value=' + lookkey + ']').attr('imgtype');
}
///绑定帮助信息
function LoadingHelperData()
{
    if(pageIndex == "" || pageIndex == "0")
        pageIndex = "1";
    $('#Category_Lookup').css('display','inline');
    $.ajax({
       type:    "POST",
       url:     '../helper/helperforactive.aspx',
       data:    {
                    otype:          'loaddata',
                    _fields:        HELP_LookUp_Key,
                    tindex:         HELP_LookUp_TableIndex,
                    findValue:      $('#findValue').val(),
                    findType:       HELP_LookUp_Otype,
                    hindex:         pageIndex,
                    imgtype:        HELP_LookUp_ImgType,
                    accountid:      $('#txtAccountId').val(),
                    productid:      $('#txtProductId').val(),
                    typecode:       $('#selectTypeCode').val()
       },
       beforeSend:function(){
            $("#divContentList").css("display","none");
            $("#divContentList").html("");
            $("#divContentlodding").css("display","block");
       },
       success: function(res, textStatus){
            if(res == "nothing")
            {
                $("#divContentlodding").css("display","none");
                $("#divContentList").css("display","block");
                $("#divContentList").html($("#divNothing").html());
            }
            else
            {
                $("#divContentlodding").css("display","none");
                $("#divContentList").css("display","block");
                $("#divContentList").html(res);
                $("#selPageIndexList").attr("value", pageIndex);
            }
       }
    }); 
}